package com.dk.util;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Properties工具类
 */
public final class PropUtil {

    /**
     * 根据Properties配置文件名获取Prop
     * @param configName
     * @return
     * @throws Exception
     */
    public static Properties getConfigProperties(String configName)throws Exception{
        Properties properties=new Properties();
        //GetResource方法默认从根目录下读取
        InputStream in =Thread.currentThread().getContextClassLoader().getResourceAsStream(configName);
        if(in==null) {
            throw new FileNotFoundException(configName+" file is not found" );
        }

        properties.load(in);
        return properties;
    }

    /**
     * 根据properties配置文件获得map
     */
    public static Map<String,Object> getMapByConfigProperties(String configName)throws Exception{
        Map<String,Object> map=new HashMap<>();
        Properties properties=getConfigProperties(configName);

        for(String name:properties.stringPropertyNames()){
            map.put(name,properties.getProperty(name));
        }
        return map;
    }
}
